package edu.zut.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;


public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应给浏览器的数据类型
        //请求以及响应编码的设定
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        //获取请求中的表单数据（请求参数）  从前端传递的表单数据，默认都是字符串
        String user_name = request.getParameter("user_name");
        System.out.println("user_name = " + user_name);
        String user_psw = request.getParameter("user_psw");
        System.out.println("user_psw = " + user_psw);
        String gender = request.getParameter("gender");
        System.out.println("gender = " + gender);
        //根据name获取对应请求参数的value，当value可能不是唯一的
        String[] habits = request.getParameterValues("habit");
        System.out.println("habits = " + Arrays.toString(habits));
        //获取请求参数的name值列表
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()) {
            System.out.println("names.nextElement() = " + names.nextElement());
        }
        //获取请求参数所有的k-v键值对
        Map<String, String[]> map = request.getParameterMap();
        map.forEach((s, strings) -> {
            System.out.println("name=" + s);
            System.out.println("values=" + Arrays.toString(strings));
        });
//        response.setCharacterEncoding("UTF-8");

        //默认提供一个响应
        response.getWriter().write("获取请求参数成功!!!!!!!!!!");

    }
}
